package com.yxhl.zoume.core.expresscar.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBarForCar;
import com.yxhl.zoume.core.expresscar.fragment.RentHourlyExpressCarFragment;
import com.yxhl.zoume.core.expresscar.fragment.SingleWayExpressCarFragment;
import com.yxhl.zoume.core.specialcar.model.CarLocationMapEntrance;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.car.DaggerExpressCarComponent;
import com.yxhl.zoume.di.component.car.ExpressCarComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.permission.PermissionHelper;

/**
 * Author：zhongshan   
 * Date：2016/12/06 11:37      
 * Description：专车首页
 */
public class ExpressCarContainerActivity extends BaseActivityWithToolBarForCar implements HasComponent<ExpressCarComponent> {

    private static final String TAG = "ExpressCarContainerActivity";
    public static final String FRAGMENT_TAG_SINGLE_WAY = "SingleWayExpressCarFragment";
    public static final String FRAGMENT_TAG_RENT_HOURLY = "RentHourlyExpressCarFragment";
    public static final String FRAGMENT_CAR_LOCATION_MAP = "OfficialCarLocationMapFragment";
    public static final String FRAGMENT_CAR_POI_SEARCH = "CarPOISearchFragment";
    public static final String FRAGMENT_SELECT_CITY = "selectCityFragment";
    private static final String YI_YANG_CITY = "益阳市";

    private String mUserSelectCity = YI_YANG_CITY;
    private ExpressCarComponent expressCarComponent;
    private String currentFragment;

    public static Intent getCallingIntent(Activity activity) {
        Intent intent = new Intent(activity, ExpressCarContainerActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeShow();
    }

    private void initializeShow() {
        addFragment(R.id.frame_express_car_content, SingleWayExpressCarFragment.newInstance(), FRAGMENT_TAG_SINGLE_WAY);
        currentFragment = FRAGMENT_TAG_SINGLE_WAY;
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    public String getUserSelectCity() {
        return mUserSelectCity;
    }

    public void setUserSelectCity(String userSelectCity) {
        mUserSelectCity = userSelectCity;
    }


    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {
    }

    @Override
    protected void onHeaderCheckedChange(RadioGroup group, int checkedId) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        switch (checkedId) {
            case R.id.rb_include_header_single_way:

                replaceFragmentWithHideShow(fragmentManager.findFragmentByTag(FRAGMENT_TAG_RENT_HOURLY),
                        fragmentManager.findFragmentByTag(FRAGMENT_TAG_SINGLE_WAY),
                        R.id.frame_express_car_content, FRAGMENT_TAG_SINGLE_WAY);

                currentFragment = FRAGMENT_TAG_SINGLE_WAY;
                break;
            case R.id.rb_include_header_rent_hourly:
                replaceFragmentWithHideShow(fragmentManager.findFragmentByTag(FRAGMENT_TAG_SINGLE_WAY),
                        getRentHourlyExpressCarFragment(fragmentManager),
                        R.id.frame_express_car_content, FRAGMENT_TAG_RENT_HOURLY);
                currentFragment = FRAGMENT_TAG_RENT_HOURLY;
                break;
        }
    }

    private Fragment getRentHourlyExpressCarFragment(FragmentManager fragmentManager) {
        Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_RENT_HOURLY);
        if (fragment == null)
            fragment = RentHourlyExpressCarFragment.newInstance();
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_express_car_container;
    }

    @Override
    protected void initializeInjector() {
        expressCarComponent = DaggerExpressCarComponent.builder()
                .activityModule(new ActivityModule(this))
                .appComponent(getAppComponent())
                .build();
    }

    @Override
    public ExpressCarComponent getComponent() {
        return expressCarComponent;
    }

    @Override
    protected void onHeadBackClick(View view) {
        onBackPressed();
    }

    public void replaceFragmentWithHideShow(Fragment fromFragment, Fragment toFragment, int containerId, String toFragmentag) {
        if (fromFragment != null && toFragment != null && containerId != 0) {
            FragmentManager fragmentManager = this.getSupportFragmentManager();

            if (fragmentManager != null) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();

                if (currentFragment.equals(FRAGMENT_TAG_SINGLE_WAY))
                    transaction.setCustomAnimations(R.anim.right_enter, R.anim.left_out, 0, 0);
                else if (currentFragment.equals(FRAGMENT_TAG_RENT_HOURLY))
                    transaction.setCustomAnimations(R.anim.left_enter, R.anim.right_out, 0, 0);

                if (!toFragment.isAdded())
                    transaction.hide(fromFragment).add(containerId, toFragment, toFragmentag).commit();
                else
                    transaction.hide(fromFragment).show(toFragment).commit();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            //单程上车地点
            case PermissionHelper.REQUEST_CODE_EXPRESS_CAR_SINGLE_WAY_GET_IN_LOCATION:
                navigateToCarLocationMapSelectActivity(grantResults[0], CarLocationMapEntrance.EXPRESS_CAR_SINGLE_WAY_GET_IN);
                break;
            //单程下车地点
            case PermissionHelper.REQUEST_CODE_EXPRESS_CAR_SINGLE_WAY_GET_OFF_LOCATION:
                navigateToCarLocationMapSelectActivity(grantResults[0], CarLocationMapEntrance.EXPRESS_CAR_SINGLE_WAY_GET_OFF);
                break;
            //分时上车地点
            case PermissionHelper.REQUEST_CODE_EXPRESS_CAR_RENT_HOURLY_GET_IN_LOCATION:
                navigateToCarLocationMapSelectActivity(grantResults[0], CarLocationMapEntrance.EXPRESS_CAR_RENT_HOURLY_GET_IN);
                break;
        }
    }

    private void navigateToCarLocationMapSelectActivity(int grantResult, CarLocationMapEntrance entrance) {
        if (grantResult == PackageManager.PERMISSION_GRANTED)
            getAppComponent().navigator().navigateToCarLoactionMapSelectActivity(this, entrance);
        else
            PromptUtils.showShort(this, getString(R.string.location_permission_start_failed));
    }
}
